<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column v-if="showColumn.procedureDesc.show" :label="showColumn.procedureDesc.title"
                       :render-header="requiredRender"
                       width="'40%'">
        <template slot-scope="scope">
          <permission-slot :read-only="showColumn.procedureDesc.readOnly" :label-name="scope.row.procedureDesc">
            <div>
              <el-input class="is-error"
                        type="textarea"
                        :rows="2"
                        placeholder=""
                        v-model="scope.row.procedureDesc">
              </el-input>
            </div>
          </permission-slot>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn.handledBy.show"
                       :label="showColumn.handledBy.title"
                       :render-header="requiredRender"
                       width="300px">
        <template slot-scope="scope">
          <permission-slot :read-only="showColumn.handledBy.readOnly" :label-name="scope.row.handledBy">
            <el-input
              type="textarea"
              :rows="2"
              placeholder=""
              v-model="scope.row.handledBy">
            </el-input>
          </permission-slot>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn.taskStatus.show"
                       :label="showColumn.taskStatus.title"
                       :render-header="requiredRender"
                       width="300px">
        <template slot-scope="scope">
          <permission-slot :read-only="isTaskStatusReadOnly(scope.row, handler)" :label-name="scope.row.taskStatus">
            <el-select size="mini" v-model="scope.row.taskStatus" placeholder="select">
              <el-option
                v-for="item in taskStatusValue"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </permission-slot>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn.taskRemark.show"
                       :label="showColumn.taskRemark.title"
                       :render-header="requiredRender"
                       width="'40%'">
        <template slot-scope="scope">
          <permission-slot :read-only="isTaskStatusReadOnly(scope.row, handler)" :label-name="scope.row.taskRemark">
            <el-input
              type="textarea"
              :rows="2"
              placeholder=""
              v-model="scope.row.taskRemark">
            </el-input>
          </permission-slot>
        </template>
      </el-table-column>
      <el-table-column
        v-if="showColumn.action.show"
        :label="showColumn.action.title" width="100">
        <template slot-scope="scope">
          <permission-slot :read-only="showColumn.action.readOnly" :label-name="''">
            <el-button
              v-show="tableData.length>1"
              size="mini"
              @click="handleRemove(scope.$index, scope.row)">Delete
            </el-button>
          </permission-slot>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="showColumn.addButton.show" class="button">
      <el-button class="button" :size="'small'" @click="handleAdd">{{showColumn.addButton.title}}</el-button>
    </div>
  </div>
</template>

<script>
import PermissionSlot from '@/base-components/permission/PermissionSlot'

export default {
  components: {PermissionSlot},
  props: {
    procedureType: {
      type: String
    },
    procedureTitle: {
      type: String
    },
    handler: {
      type: String
    },
    showColumn: {
      type: Object,
      default: () => {
        return {
          procedureDesc: {
            title: 'test',
            show: true,
            readOnly: true
          },
          handledBy: {
            title: 'test',
            show: true,
            readOnly: true
          },
          taskStatus: {
            title: 'test',
            show: true,
            readOnly: true
          },
          taskRemark: {
            title: 'Test',
            show: true,
            readOnly: true
          },
          action: {
            title: 'Action',
            show: false,
            readOnly: false
          },
          addButton: {
            show: false,
            title: 'Add New Procedure'
          }
        }
      }
    },
    tableData: {
      default: () => [
        {
          procedureDesc: '2016-05-02',
          handledBy: '王小虎',
          taskStatus: 'test1',
          taskRemark: ''
        }
      ]
    }
  },
  data() {
    return {
      taskStatusValue: [
        'test', 'test1', 'test2', 'test3', 'test4'
      ]
    }
  },
  methods: {
    setTaskStatusList(taskList) {
      this.taskStatusValue = taskList
    },
    isValid() {
      return this.tableData.every((item) => {
        if (!item.procedureDesc || !item.handledBy) {
          this.$message({showClose: true, message: `${this.procedureTitle} can not be null`, type: 'error'})
          return false
        }
        return true
      })
    },
    isTaskStatusReadOnly(row, handler) {
      if (!this.showColumn.taskStatus.readOnly) {
        return false
      }
      if (!handler || handler.toLowerCase().indexOf(row.handledBy.toLowerCase().trim()) < 0) {
        return true
      }
      return false
    },
    handleAdd() {
      this.tableData.push({
        procedureDesc: '',
        handledBy: '',
        taskStatus: '',
        taskRemark: '',
        procedureType: this.procedureType
      })
    },
    handleRemove(index, row) {
      this.tableData.splice(index, 1)
    },
    getProcedureData() {
      return this.tableData
    },
    requiredRender: function (h, header, title) {
      if (header.$index < 2) {
        return (
          <span>
            {header.column.label}
            <span style='color:red;'> * </span>
          </span>
        )
      }
      return (
        <span>{header.column.label}</span>
      )
    }
  }
}
</script>

<style scoped>
  .button {
    margin: 5px 0 5px 5px;
  }

  .is-error .el-input__inner {
    border-color: #f56c6c;
  }
</style>
